var deviceone = require("deviceone");

module.exports.timeFormat = function(svTime){
	if (svTime == null) {
		return "";
	}
	var minute = 1000 * 60;
	var hour = minute * 60;	
	var now = new Date();//现在
	var svDate = new Date(svTime);//要格式化的时间	
    var nowTime = now.getTime();
    var diffTime = nowTime - svTime;//时间差    
    var diffHour = parseInt(diffTime / hour);//相差-小时
    var diffMinutes = parseInt(diffTime / minute);//相差-分钟
    
    var isSameDay = (svDate.getDate()==now.getDate() ? true : false);//“日” 是否同
    var isSameYear = (svDate.getFullYear()==now.getFullYear() ? true : false);//“年” 是否同
    
	if (diffMinutes < 0) {//如果是未来的时间，显示yyyy-MM-dd HH:mm
		return svDate.getFullYear()+'-'+(svDate.getMonth()+1)+'-'+svDate.getDate()+' '+svDate.getHours()+':'+svDate.getMinutes();
	} else if (diffMinutes < 1) {
		return "刚刚";
	} else if (diffMinutes < 60) {
		return diffMinutes + "分钟前";
	} else if (diffHour >= 1 && diffHour < 24) {//24小时内
		return diffHour + "小时前";
	} else if (diffHour >= 24 && isSameYear) {// 本年内，显示 MM-dd HH:mm
		return (svDate.getMonth()+1)+'-'+svDate.getDate()+' '+svDate.getHours()+':'+svDate.getMinutes();
	} else {//超过一年显示完整日期yyyy-MM-dd
		return svDate.getFullYear()+'-'+(svDate.getMonth()+1)+'-'+svDate.getDate();
	}
};

var showMonth,showDay,showHour,showMin = "00";//显示的【两个字符】

module.exports.shortFormat = function(svTime){
	if (svTime == null) {
		return "";
	}
	var minute = 1000 * 60;
	var hour = minute * 60;	
	var now = new Date();//现在
	var svDate = new Date(svTime);//要格式化的时间	
    var nowTime = now.getTime();
    var diffTime = nowTime - svTime;//时间差    
    var diffHour = parseInt(diffTime / hour);//相差-小时
    var diffMinutes = parseInt(diffTime / minute);//相差-分钟
    
    var isSameDay = (svDate.getDate()==now.getDate() ? true : false);//“日” 是否同
    var isSameYear = (svDate.getFullYear()==now.getFullYear() ? true : false);//“年” 是否同
    
    showMonth = (svDate.getMonth()+1)+"";//显示的-月
    if(showMonth.length == 1){
    	showMonth = "0"+""+showMonth;
    }
    showDay = svDate.getDate()+"";//显示的-日
    if(showDay.length == 1){
    	showDay = "0"+""+showDay;
    }
    showHour = svDate.getHours()+"";//显示的-时
    if(showHour.length == 1){
    	showHour = "0"+""+showHour;
    }
    showMin = svDate.getMinutes()+"";//显示的-分
    if(showMin.length == 1){
    	showMin = "0"+""+showMin;
    }
	if (diffMinutes < 0) {//如果是未来的时间，显示yyyy-MM-dd HH:mm
		return svDate.getFullYear()+'-'+showMonth+'-'+showDay+' '+showHour+':'+showMin;
	} else if(diffHour < 24) {//24小时内
		if(isSameDay){
			return showHour+':'+showMin;
		}else{
			return '昨天 '+showHour+':'+showMin;
		}
	} else if (diffHour >= 24 && isSameYear) {// 本年内，显示 MM-dd HH:mm
		return showMonth+'-'+showDay+' '+showHour+':'+showMin;
	} else {//超过一年显示完整日期yyyy-MM-dd
		return svDate.getFullYear()+'-'+showMonth+'-'+showDay;
	}
};